package com.sds.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.sds.fw.dao.AbDao;
import com.sds.shop.vo.OrderVO;
import com.sds.shop.vo.PaymentVO;
import com.sds.shop.vo.ProductVO;

public class PaymentDAO extends AbDao{
	
	private static PaymentDAO dao = new PaymentDAO();
	public static PaymentDAO getInstance(){
		return dao;
	}
	
	public void doPay(OrderVO vo){
		
		Connection conn = null;
		PreparedStatement ps = null;
		
		try {
			conn = connect();

			StringBuffer query = new StringBuffer();
			query.append("insert into tb_payment(pay_no, ord_no, payment_opt_cd, pay_amt, pay_dtm) ");
			query.append("values (?,?,?,?,to_char(sysdate, 'YYYYMMDDHH24MISS'))");
			
			ps = conn.prepareStatement(query.toString());
			String pay_no = ""+(dao.getPayList("").size()+1);
			ps.setString(1, pay_no);
			ps.setString(2, vo.getOrd_no());
			ps.setString(3, "C01");
			ps.setInt(4, Integer.parseInt(vo.getOrderprice()));
			ps.executeUpdate();
			
			ps.close();
			conn.close();
			
		} catch (Exception ex) {
			System.out.println("insertPay exception : " + ex);
			close(conn, ps);
		}
	}
	
	public PaymentVO getPayVO(String ord_no){
		PaymentVO pvo = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = connect();
			
			StringBuffer query = new StringBuffer();
			query.append("Select pay_no, ord_no, payment_opt_cd, pay_amt, pay_dtm ");
			query.append("from tb_payment ");
			query.append("where ord_no= ? ");
			ps=conn.prepareStatement(query.toString());
			ps.setString(1, ord_no);			 		
			
			rs=ps.executeQuery();
			while(rs.next()){
				pvo = new PaymentVO();
				pvo.setPay_no(rs.getString(1));
				pvo.setOrd_no(rs.getString(2));
				pvo.setPayment_opt_cd(rs.getString(3));
				pvo.setPay_amt(rs.getInt(4));
				pvo.setPay_dtm(rs.getString(5));
				
				close(conn, ps, rs);
				return pvo;
			}
						
		} catch (Exception ex) {
			System.out.println("getPayList exception : " + ex);
			close(conn, ps, rs);
		}
		return null;
		
	}
	
	public boolean isPayVO(String ord_no){
		PaymentVO pvo = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = connect();
			
			StringBuffer query = new StringBuffer();
			query.append("Select pay_no, ord_no, payment_opt_cd, pay_amt, pay_dtm ");
			query.append("from tb_payment ");
			query.append("where ord_no= ? ");
			ps=conn.prepareStatement(query.toString());
			ps.setString(1, ord_no);			 		
			
			rs=ps.executeQuery();
			while(rs.next()){
				rs.close();
				ps.close();
				conn.close();
				
				return true;
			}
			
			rs.close();
			ps.close();
			conn.close();
			
		} catch (Exception ex) {
			System.out.println("getPayList exception : " + ex);
			close(conn, ps, rs);
		}
		return false;
		
	}

	public ArrayList<PaymentVO> getPayList(String ord_no){
		ArrayList<PaymentVO> list = new ArrayList<PaymentVO>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = connect();
			
			StringBuffer query = new StringBuffer();
			query.append("Select pay_no, ord_no, payment_opt_cd, pay_amt, pay_dtm ");
			query.append("from tb_payment ");
			
			if( "ALL".equals( ord_no ) || ord_no == null || ord_no.equals("")){ 
				ps = conn.prepareStatement( query.toString() );				
			} else {
				query.append("where ord_no= ? ");
				ps=conn.prepareStatement(query.toString());
				ps.setString(1, ord_no);			 
			}			
			
			rs=ps.executeQuery();
			while(rs.next()){
				PaymentVO pvo = new PaymentVO();
				pvo.setPay_no(rs.getString(1));
				pvo.setOrd_no(rs.getString(2));
				pvo.setPayment_opt_cd(rs.getString(3));
				pvo.setPay_amt(rs.getInt(4));
				pvo.setPay_dtm(rs.getString(5));
				
				list.add(pvo);
			}
			
			rs.close();
			ps.close();
			conn.close();
			
		} catch (Exception ex) {
			System.out.println("getPayList exception : " + ex);
			close(conn, ps, rs);
		}
		return list;
		
	}

}
